package com.project.rfidpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.rfidpage.dto.CenterLinkDto;
import com.project.rfidpage.dto.DataShareLinkDto;
import com.project.rfidpage.dto.SearchByMonthParam;
import com.project.rfidpage.dto.SearchByRqParam;
import com.project.rfidpage.entity.CenterLink;
import com.project.rfidpage.mapper.CenterLinkMapper;
import com.project.rfidpage.service.CenterLinkService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class CenterLinkServiceImpl implements CenterLinkService {

    @Resource
    private CenterLinkMapper centerLinkMapper;

    @Override
    @DS("third")
    public CenterLinkDto getForPage() {
        CenterLinkDto dto = centerLinkMapper.selectCenterLink();
        if (dto != null) {
            List<CenterLink> list = centerLinkMapper.selectCenterLinkDetail();
            if (list.size() > 0) {
                int size = list.size();
                int link = list.stream().mapToInt(CenterLink::getLinked).sum();
                int count = list.stream().mapToInt(CenterLink::getAmount).sum();
                BigDecimal online = new BigDecimal(link).divide(new BigDecimal(size), 1, RoundingMode.HALF_UP);
                dto.setOnline(String.valueOf(online));
                dto.setCount(count);
            }
        }
        return dto;
    }

    @Override
    @DS("third")
    public List<CenterLink> getDetailForPage() {
        return centerLinkMapper.selectCenterLinkDetail();
    }

    @Override
    @DS("third")
    public List<DataShareLinkDto> getDataShareForPage() {
        return centerLinkMapper.selectDataShareLink();
    }

    @Override
    @DS("third")
    public List<CenterLinkDto> getListByMonthForPage(SearchByMonthParam param) {
        return centerLinkMapper.selectCenterLinkByMonth(param);
    }

    @Override
    @DS("third")
    public List<CenterLinkDto> getListByRqForPage(SearchByRqParam param) {
        return centerLinkMapper.selectCenterLinkByRq(param);
    }
}
